package com.btshop.goods.controller;
import com.btshop.goods.pojo.Pref;
import com.btshop.goods.service.PrefService;
import com.github.pagehelper.PageInfo;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: PrefController
 * @description: Pref 控制层接口定义类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Api(value = "PrefController")
@Slf4j
@RestController
@RequestMapping("/pref")
@CrossOrigin
public class PrefController {

    @Autowired
    private PrefService prefService;

    /***
     * Pref分页条件搜索实现
     * @param pref
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Pref条件分页查询",notes = "分页条件查询Pref方法详情",tags = {"PrefController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Pref对象",value = "传入JSON数据",required = false) Pref pref, @PathVariable  int page, @PathVariable  int size){
        log.info("PrefController findPage params , pref : {} , page : {} , size : {}",pref,page,size);
        //调用PrefService实现分页条件查询Pref
        PageInfo<Pref> pageInfo = prefService.findPage(pref, page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * Pref分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Pref分页查询",notes = "分页查询Pref方法详情",tags = {"PrefController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        log.info("PrefController findPage params , page : {} , size : {}",page,size);
        //调用PrefService实现分页查询Pref
        PageInfo<Pref> pageInfo = prefService.findPage(page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param pref
     * @return
     */
    @ApiOperation(value = "Pref条件查询",notes = "条件查询Pref方法详情",tags = {"PrefController"})
    @PostMapping(value = "/search" )
    public Result<List<Pref>> findList(@RequestBody(required = false) @ApiParam(name = "Pref对象",value = "传入JSON数据",required = false) Pref pref){
        log.info("PrefController findList params , pref : {} ",pref);
        //调用PrefService实现条件查询Pref
        List<Pref> list = prefService.findList(pref);
        return Result.buildSuccess("查询成功！", list);
    }

    /***
     * 根据主键删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Pref根据主键id删除",notes = "根据主键删除Pref方法详情",tags = {"PrefController"})
    @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Long id){
        log.info("PrefController delete params ,   id : {}   ",id);
        //调用PrefService实现根据主键删除
        prefService.delete(id);
        return Result.buildSuccess("删除成功！");
    }

    /***
     * 修改Pref数据
     * @param pref
     * @param id
     * @return
     */
    @ApiOperation(value = "Pref根据id修改",notes = "根据ID修改Pref方法详情",tags = {"PrefController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
     })
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Pref对象",value = "传入JSON数据",required = false) Pref pref,@PathVariable("id") Long id){
        log.info("PrefController update params ,  pref : {},  id : {}   ",pref,id);
        //设置主键值
        pref.setId(id);
        //调用PrefService实现修改Pref
        prefService.update(pref);
        return Result.buildSuccess("修改成功！");
    }

    /***
     * 新增Pref数据
     * @param pref
     * @return
     */
    @ApiOperation(value = "Pref添加",notes = "添加Pref方法详情",tags = {"PrefController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Pref对象",value = "传入JSON数据",required = true) Pref pref){
        log.info("PrefController add params , pref : {}",pref);
        //调用PrefService实现添加Pref
        prefService.add(pref);
        return Result.buildSuccess("添加成功！");
    }

    /***
     * 根据ID查询Pref数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Pref根据id查询",notes = "根据ID查询Pref方法详情",tags = {"PrefController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
            })
    @GetMapping("/{id}")
    public Result<Pref> findById(@PathVariable("id") Long id){
        log.info("PrefController findById params ,   id : {}   ",id);
        //调用PrefService实现根据主键查询Pref
        Pref pref = prefService.findById(id);
        return Result.buildSuccess("查询成功！",pref);
    }

    /***
     * 查询Pref全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Pref",notes = "查询所Pref有方法详情",tags = {"PrefController"})
    @GetMapping
    public Result<List<Pref>> findAll(){
        //调用PrefService实现查询所有Pref
        List<Pref> list = prefService.findAll();
        return Result.buildSuccess("查询成功！",list);
    }
}
